﻿using MaterialDesignThemes.Wpf;
using Microsoft.Extensions.Logging;
using MyMeeting.Common;
using MyMeeting.Dtos;
using MyMeeting.Extensions;
using MyMeeting.Views;
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;

namespace MyMeeting.ViewModels
{
    public class MainWindowViewModel : BindableBase, IConfigService
    {
        private string _title = "Prism Application";
        public string Title
        {
            get { return _title; }
            set {
                RaisePropertyChanged();
                // SetProperty(ref _title, value);
            }
        }
        private readonly IDialogService dialogHost;
        IRegionManager regionManager = null;

       // private readonly Logger<MainWindow> logger;
        public MainWindowViewModel(IRegionManager rmanager,IDialogService dialogService)
        {
            this.regionManager = rmanager;
            MenuItemClickCommand = new DelegateCommand<NavMenuDto>(OpenMenu);
            FuncItemClickCommand = new DelegateCommand<NavFuncDto>(OpenDialog);
            CreateTestMenuDtos();
            CreateTestFuncDtos();
            this.dialogHost = dialogService;
            //InitCommand = new DelegateCommand(() =>
            //{
            //    this.regionManager.Regions[PrismManager.RegionName].RequestNavigate(menuDtos[0].GoToNavPage);
            //});
         //   logger.LogDebug("日志测试");
        }

        private void OpenDialog(NavFuncDto dto)
        {
            if (dto == null || string.IsNullOrWhiteSpace(dto.DialogName))
                return;
            DialogOpenedEventHandler eventHandler = (sender, eventArgs) =>
            {
                var a = sender;
            };
            dialogHost.Show(dto.DialogName);
        }

        private void OpenMenu(NavMenuDto dto)
        {
            if(dto==null||string.IsNullOrWhiteSpace(dto.GoToNavPage))
            {
                return;
            }
            regionManager.Regions[PrismManager.RegionName].RequestNavigate(dto.GoToNavPage);
        }

        private ObservableCollection<NavMenuDto> menuDtos = null;
        private ObservableCollection<NavFuncDto> funcDto = null;

        public DelegateCommand<NavMenuDto> MenuItemClickCommand { get; }
        public DelegateCommand<NavFuncDto> FuncItemClickCommand { get; }

        public DelegateCommand InitCommand { get; }
        public ObservableCollection<NavMenuDto> MenuDtos
        {
            get { return menuDtos; }
            set { menuDtos = value; RaisePropertyChanged(); }
        }
        public ObservableCollection<NavFuncDto> FuncDtos
        {
            get { return funcDto; }
            set { funcDto = value; RaisePropertyChanged(); }
        }
        public void CreateTestMenuDtos()
        {
            MenuDtos = new ObservableCollection<NavMenuDto>();
            MenuDtos.Add(new NavMenuDto()
            {
                IconName = "Video",
                IconText = "会议",
                GoToNavPage = "MeetingView"
            });
            MenuDtos.Add(new NavMenuDto()
            {
                IconName = "BookOpenBlankVariant",
                IconText = "通讯录",
                GoToNavPage = "ContactsView"
            });
            MenuDtos.Add(new NavMenuDto()
            {
                IconName = "RecordCircle",
                IconText = "录制",
                GoToNavPage = "RecordView"
            });
        }

        public void CreateTestFuncDtos()
        {
            FuncDtos = new ObservableCollection<NavFuncDto>();

            FuncDtos.Add(new NavFuncDto()
            {
                IconName = "EmailOutline",
                IconText = "邮箱",
                DialogName = "OpenEmailView"
            });
            FuncDtos.Add(new NavFuncDto()
            {
                IconName = "CogOutline",
                IconText = "设置",
                DialogName = "OpenSettings"
            });
            FuncDtos.Add(new NavFuncDto()
            {
                IconName = "AccountOutline",
                IconText = "账户",
                DialogName = "OpenAccount"
            });
        }

        public void Loaded()
        {
            this.regionManager.Regions[PrismManager.RegionName].RequestNavigate(menuDtos[0].GoToNavPage);
        }
    }
}
